# Maintainer: David Runge <dvzrv@archlinux.org>

_name=DPF-Plugins
pkgbase=dpf-plugins
pkgname=(
  dpf-plugins
  dpf-plugins-{clap,dssi,ladspa,lv2,standalone,vst,vst3}
)
pkgver=1.7
pkgrel=1
pkgdesc="Collection of DPF-based plugins"
arch=(loong64 x86_64)
url="https://github.com/DISTRHO/DPF-Plugins"
license=(
  ISC
  GPL2
  GPL3
  LGPL3
  MIT
)
makedepends=(
  dssi
  gendesk
  jack
  ladspa
  libglvnd
  lv2
  projectm
)
checkdepends=(
  lv2lint
  kxstudio-lv2-extensions
)
source=($pkgname-$pkgver.tar.gz::https://github.com/DISTRHO/$pkgname/archive/v$pkgver.tar.gz)
sha512sums=('7d84be61f5c173bd80ee2f94e41384fc08a18a651d995aec20345d19a80e7a2e771b6f9eb5de9c03acb4368a2a071f915373f6cca89b2075f1f096f4000c3147')
b2sums=('fa60ae8aa75065c97fbb56ca02e084d4077021ba9953d593af50636f7a8ec8f3a9dce901a0746f0e33963d7c58bae2cbad47c892e2f491147f41cff7b4eecb53')

_plugins=(
  3BandEQ
  3BandSplitter
  AmplitudeImposer
  CycleShifter
  Kars
  MVerb
  MaBitcrush
  MaFreeverb
  MaGigaverb
  MaPitchshift
  Nekobi
  PingPongPan
  SoulForce
  glBars
)

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  declare -A _comments=(
    ["3BandEQ"]="A three band equalizer"
    ["3BandSplitter"]="A three band splitter"
    ["AmplitudeImposer"]="Takes 2 stereo inputs and imposes the amplitude envelope of the first one on the second one"
    ["CycleShifter"]="Reads in a cycle's-worth of the input signal, then (once the whole cycle's been read in) outputs it again, on top of the current output"
    ["Kars"]="Simple karplus-strong plucked string synth"
    ["MVerb"]="A studio quality, open-source reverb"
    ["MaBitcrush"]="A bit crusher"
    ["MaFreeverb"]="A reverb based on freeverb"
    ["MaGigaverb"]="A reverb based on gigaverb"
    ["MaPitchshift"]="A pitch shifter"
    ["Nekobi"]="A simple single-oscillator synth based on the Roland TB-303"
    ["PingPongPan"]="A Ping Pong Panner"
    ["SoulForce"]="A fairly standard waveshaping distortion plugin"
    ["glBars"]="An OpenGL bars visualization plugin"
  )
  declare -A _generic=(
    ["3BandEQ"]="Equalizer"
    ["3BandSplitter"]="Splitter"
    ["AmplitudeImposer"]="Wave Shaper"
    ["CycleShifter"]="Cyclic Input Processor"
    ["Kars"]="Plucked string synthesizer"
    ["MVerb"]="Reverb"
    ["MaBitcrush"]="Bit crusher"
    ["MaFreeverb"]="Reverb"
    ["MaGigaverb"]="Reverb"
    ["MaPitchshift"]="Pitch shifter"
    ["Nekobi"]="Synthesizer"
    ["PingPongPan"]="Ping Pong Panner"
    ["SoulForce"]="Waveshaping Distortion"
    ["glBars"]="Bars Visualization"
  )
  local _plugin

  for _plugin in "${_plugins[@]}"; do
    gendesk -n \
            --pkgname "net.sf.distrho.$_plugin" \
            --name "$_plugin" \
            --exec "/usr/bin/$_plugin" \
            --pkgdesc "${_comments[$_plugin]}" \
            --genericname "${_generic[$_plugin]}" \
            --categories "AudioVideo;Audio"
  done
}

build() {
  make V=1 -C $_name-$pkgver
}

check() {
  declare -A _uris=(
    ["3BandEQ"]="http://distrho.sf.net/plugins/3BandEQ"
    ["3BandSplitter"]="http://distrho.sf.net/plugins/3BandSplitter"
    ["AmplitudeImposer"]="http://www.niallmoody.com/ndcplugs/ampimposer.htm"
    ["CycleShifter"]="http://www.niallmoody.com/ndcplugs/cycleshifter.htm"
    ["Kars"]="http://distrho.sf.net/plugins/Kars"
    ["MVerb"]="http://distrho.sf.net/plugins/MVerb"
    ["MaBitcrush"]="http://distrho.sf.net/plugins/MaBitcrush"
    ["MaFreeverb"]="http://distrho.sf.net/plugins/MaFreeverb"
    ["MaGigaverb"]="http://distrho.sf.net/plugins/MaGigaverb"
    ["MaPitchshift"]="http://distrho.sf.net/plugins/MaPitchshift"
    ["Nekobi"]="http://distrho.sf.net/plugins/Nekobi"
    ["PingPongPan"]="http://distrho.sf.net/plugins/PingPongPan"
    ["SoulForce"]="http://www.niallmoody.com/ndcplugs/soulforce.htm"
    ["glBars"]="http://distrho.sf.net/plugins/glBars"
  )
  local _plugin

  cd $_name-$pkgver
  # except symbol pattern from tests as it is hard to remove according to upstream
  for _plugin in "${_plugins[@]}"; do
    printf "Using lv2lint on %s: %s\n" "$_plugin" "${_uris[$_plugin]}"

    # disable Plugin Class tests because of CyclicShifter: https://github.com/DISTRHO/DPF-Plugins/issues/22
    lv2lint -s "lv2_generate_ttl" -t "Plugin Class" -Mpack -I bin/$_plugin.lv2/ "${_uris[$_plugin]}"
  done
}

package_dpf-plugins() {
  depends=(
    $pkgbase-{clap,dssi,ladspa,lv2,standalone,vst,vst3}=$pkgver
  )

  cd $_name-$pkgver
  make V=1 DESTDIR="$pkgdir/" PREFIX=/usr install

  (
    cd "$pkgdir"
    # have find-libdeps resolve dependencies properly:
    # https://gitlab.archlinux.org/archlinux/devtools/-/issues/102
    find usr/lib -type f \( -iname "*.clap" -or -iname "*.so" \) -exec chmod +x {} \;

    _pick $pkgbase-clap usr/lib/clap/
    _pick $pkgbase-dssi usr/lib/dssi/
    _pick $pkgbase-ladspa usr/lib/ladspa/
    _pick $pkgbase-lv2 usr/lib/lv2/
    _pick $pkgbase-standalone usr/bin/
    _pick $pkgbase-vst usr/lib/vst/
    _pick $pkgbase-vst3 usr/lib/vst3/
  )

}

package_dpf-plugins-clap() {
  pkgdesc+=" - CLAP plugins"
  groups=(clap-plugins pro-audio)
  depends=(
    clap-host
    gcc-libs
    glibc
    libglvnd
    libx11
    libxext
  )
  optdepends=(
    'projectm: for ProM plugin'
  )

  mv $pkgname/* "$pkgdir"
  install -vDm 644 $_name-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 $_name-$pkgver/dpf/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/dpf/"
  install -vDm 644 $_name-$pkgver/plugins/Kars/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/Kars/"
  install -vDm 644 $_name-$pkgver/plugins/AmplitudeImposer/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/AmplitudeImposer/"
  install -vDm 644 $_name-$pkgver/plugins/CycleShifter/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/CycleShifter/"
  install -vDm 644 $_name-$pkgver/plugins/SoulForce/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/SoulForce/"
  find "$pkgdir" -type f -iname "*.clap" -exec chmod +x {} +
}

package_dpf-plugins-dssi() {
  pkgdesc+=" - DSSI plugins"
  groups=(dssi-plugins pro-audio)
  depends=(
    dssi-host
    gcc-libs
    glibc
    libglvnd
    liblo
    libx11
    libxext
  )

  mv $pkgname/* "$pkgdir"
  install -vDm 644 $_name-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 $_name-$pkgver/dpf/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/dpf/"
  install -vDm 644 $_name-$pkgver/plugins/Kars/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/Kars/"
  install -vDm 644 $_name-$pkgver/plugins/AmplitudeImposer/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/AmplitudeImposer/"
  install -vDm 644 $_name-$pkgver/plugins/CycleShifter/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/CycleShifter/"
  install -vDm 644 $_name-$pkgver/plugins/SoulForce/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/SoulForce/"
  find "$pkgdir" -type f -iname "*.so" -exec chmod +x {} +
}

package_dpf-plugins-ladspa() {
  pkgdesc+=" - LADSPA plugins"
  groups=(ladspa-plugins pro-audio)
  depends=(
    gcc-libs
    glibc
    ladspa-host
  )

  mv $pkgname/* "$pkgdir"
  install -vDm 644 $_name-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 $_name-$pkgver/dpf/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/dpf/"
  install -vDm 644 $_name-$pkgver/plugins/AmplitudeImposer/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/AmplitudeImposer/"
  install -vDm 644 $_name-$pkgver/plugins/CycleShifter/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/CycleShifter/"
  install -vDm 644 $_name-$pkgver/plugins/SoulForce/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/SoulForce/"
  find "$pkgdir" -type f -iname "*.so" -exec chmod +x {} +
}

package_dpf-plugins-lv2() {
  pkgdesc+=" - LV2 plugins"
  groups=(lv2-plugins pro-audio)
  depends=(
    gcc-libs
    glibc
    lv2-host
    libglvnd
    libx11
    libxext
  )
  optdepends=(
    'projectm: for ProM plugin'
  )

  mv $pkgname/* "$pkgdir"
  install -vDm 644 $_name-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 $_name-$pkgver/dpf/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/dpf/"
  install -vDm 644 $_name-$pkgver/plugins/Kars/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/Kars/"
  install -vDm 644 $_name-$pkgver/plugins/AmplitudeImposer/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/AmplitudeImposer/"
  install -vDm 644 $_name-$pkgver/plugins/CycleShifter/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/CycleShifter/"
  install -vDm 644 $_name-$pkgver/plugins/SoulForce/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/SoulForce/"
  find "$pkgdir" -type f -iname "*.so" -exec chmod +x {} +
}

package_dpf-plugins-standalone() {
  pkgdesc+=" - standalones"
  depends=(
    alsa-lib
    gcc-libs
    glibc
    libglvnd
    libx11
    libxext
  )
  optdepends=(
    'jack: for using JACK as backend'
  )

  mv $pkgname/* "$pkgdir"
  install -vDm 644 *.desktop -t "$pkgdir/usr/share/applications/"
  install -vDm 644 $_name-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 $_name-$pkgver/dpf/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/dpf/"
  install -vDm 644 $_name-$pkgver/plugins/Kars/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/Kars/"
  install -vDm 644 $_name-$pkgver/plugins/AmplitudeImposer/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/AmplitudeImposer/"
  install -vDm 644 $_name-$pkgver/plugins/CycleShifter/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/CycleShifter/"
  install -vDm 644 $_name-$pkgver/plugins/SoulForce/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/SoulForce/"
}

package_dpf-plugins-vst() {
  pkgdesc+=" - VST2 plugins"
  groups=(pro-audio vst-plugins)
  depends=(
    gcc-libs
    glibc
    libglvnd
    libx11
    libxext
    vst-host
  )
  optdepends=(
    'projectm: for ProM plugin'
  )

  mv $pkgname/* "$pkgdir"
  install -vDm 644 $_name-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 $_name-$pkgver/dpf/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/dpf/"
  install -vDm 644 $_name-$pkgver/plugins/Kars/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/Kars/"
  install -vDm 644 $_name-$pkgver/plugins/AmplitudeImposer/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/AmplitudeImposer/"
  install -vDm 644 $_name-$pkgver/plugins/CycleShifter/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/CycleShifter/"
  install -vDm 644 $_name-$pkgver/plugins/SoulForce/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/SoulForce/"
  find "$pkgdir" -type f -iname "*.so" -exec chmod +x {} +
}

package_dpf-plugins-vst3() {
  pkgdesc+=" - VST3 plugins"
  groups=(pro-audio vst3-plugins)
  depends=(
    gcc-libs
    glibc
    libglvnd
    libx11
    libxext
    vst3-host
  )
  optdepends=(
    'projectm: for ProM plugin'
  )

  mv $pkgname/* "$pkgdir"
  install -vDm 644 $_name-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 $_name-$pkgver/dpf/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/dpf/"
  install -vDm 644 $_name-$pkgver/plugins/Kars/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/Kars/"
  install -vDm 644 $_name-$pkgver/plugins/AmplitudeImposer/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/AmplitudeImposer/"
  install -vDm 644 $_name-$pkgver/plugins/CycleShifter/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/CycleShifter/"
  install -vDm 644 $_name-$pkgver/plugins/SoulForce/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/SoulForce/"
  find "$pkgdir" -type f -iname "*.so" -exec chmod +x {} +
}
